//
//  ICMProxyBasicInfoView.m
//  ICrEAM
//
//  Created by kuroro on 16/11/22.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMProxyBasicInfoView.h"
#import "AppDelegate.h"
#import "PACACalendarView.h"
#import "NSDate+convenience.h"

@interface ICMProxyBasicInfoView ()<ICMTitleTextFeildViewDelegate>
@property (nonatomic, strong) NSDate *selectedDate;
@property (nonatomic, strong) TPKeyboardAvoidingScrollView *bigScrollView;
@end

@implementation ICMProxyBasicInfoView

#define xDistanceA                  ActualWidth(25)
#define xDistanceB                  ActualWidth(50)
#define yDistance                   (ScreenHeight-240-6*TitleTFViewHeight)/6-3
#define TitleTFViewHeight           ActualHeight(63)
#define TitleTViewHeight            ActualHeight(90)

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        self.bigScrollView = [[TPKeyboardAvoidingScrollView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame))];
        self.bigScrollView.backgroundColor = [UIColor clearColor];
        self.selectedDate=[NSDate date];
        self.backgroundColor = BackgroundColor_White;
        //第一行
        ICMTitleTextFeildView *vinNumberView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(ActualWidth(20), yDistance, ActualWidth(220), TitleTFViewHeight) title:@"VIN"];
        [self.bigScrollView addSubview:vinNumberView];
        
        
        ICMTitleTextFeildView *plateNumberView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(vinNumberView.rightEdge+xDistanceA, vinNumberView.y, ActualWidth(220), TitleTFViewHeight) title:@"车牌号"];
        [self.bigScrollView addSubview:plateNumberView];
        
        ICMTitleTextFeildView *ownerNameView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(plateNumberView.rightEdge+xDistanceB, plateNumberView.y, ActualWidth(465), TitleTFViewHeight) title:@"车主姓名"];
        [self.bigScrollView addSubview:ownerNameView];
        
        self.vinNumberView = vinNumberView;
        self.plateNumberView = plateNumberView;
        self.ownerNameView = ownerNameView;
        
        //第二行
        ICMTitleTextFeildView *invoiceDateView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(vinNumberView.x, vinNumberView.bottomEdge+yDistance, ActualWidth(220), TitleTFViewHeight) title:@"购车发票日期"];
        [self.bigScrollView addSubview:invoiceDateView];
        
        ICMTitleTextFeildView *currentMileageView = [ICMTitleTextFeildView createEditableICMTitleTextFeildViewWithFrame:CGRectMake(invoiceDateView.rightEdge+xDistanceA, invoiceDateView.y, ActualWidth(220), TitleTFViewHeight) title:@"当前行驶里程(KM)"];
        currentMileageView.delegate = self;
        [self.bigScrollView addSubview:currentMileageView];
        
        ICMTitleTextFeildView *ownerPhoneNumberView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(currentMileageView.rightEdge+xDistanceB, currentMileageView.y, ActualWidth(220), TitleTFViewHeight) title:@"车主手机"];
        [self.bigScrollView addSubview:ownerPhoneNumberView];
        
        ICMTitleTextFeildView *ownerTypeView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(ownerPhoneNumberView.rightEdge+xDistanceA, ownerPhoneNumberView.y, ActualWidth(220), TitleTFViewHeight) title:@"车主性质"];
        [self.bigScrollView addSubview:ownerTypeView];
        
        self.invoiceDateView = invoiceDateView;
        self.currentMileageView = currentMileageView;
        self.ownerPhoneNumberView = ownerPhoneNumberView;
        self.ownerTypeView = ownerTypeView;
        
        
        //第三行
        ICMTitleTextFeildView *carTypeView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(invoiceDateView.x, invoiceDateView.bottomEdge+yDistance, ActualWidth(465), TitleTFViewHeight) title:@"车型"];
        [self.bigScrollView addSubview:carTypeView];
        
        ICMTitleTextFeildView *customerNameView = [ICMTitleTextFeildView createEditableICMTitleTextFeildViewWithFrame:CGRectMake(ownerPhoneNumberView.x, carTypeView.y, ActualWidth(220), TitleTFViewHeight) title:@"送修人姓名"];
        customerNameView.delegate = self;
        [self.bigScrollView addSubview:customerNameView];
        
        ICMTitleTextFeildView *customerPhoneNumberView = [ICMTitleTextFeildView createEditableICMTitleTextFeildViewWithFrame:CGRectMake(customerNameView.rightEdge+xDistanceA, customerNameView.y, ActualWidth(220), TitleTFViewHeight) title:@"送修人手机"];
        customerPhoneNumberView.delegate = self;
        [self.bigScrollView addSubview:customerPhoneNumberView];
        
        self.carTypeView = carTypeView;
        self.customerNameView = customerNameView;
        self.customerPhoneNumberView = customerPhoneNumberView;
        
        //第四行
        ICMTitleTextFeildView *useKindView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(vinNumberView.x, carTypeView.bottomEdge+yDistance, ActualWidth(220), TitleTFViewHeight) title:@"使用性质"];
        [self.bigScrollView addSubview:useKindView];
        
        ICMTitleTextFeildView *traceTagView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(currentMileageView.x, useKindView.y, ActualWidth(220), TitleTFViewHeight) title:@"三包凭证标识"];
        [self.bigScrollView addSubview:traceTagView];
        
        ICMTitleTextFeildView *VWCardView = nil;
        if([ICrEAMTool isVW])
        {
            //大众
            VWCardView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(currentMileageView.rightEdge+xDistanceB, useKindView.y, ActualWidth(220), TitleTFViewHeight) title:@"大众一家"];
        }
        else
        {
            //斯柯达
            VWCardView = [ICMTitleTextFeildView createEditableICMTitleTextFeildViewWithFrame:CGRectMake(currentMileageView.rightEdge+xDistanceB, useKindView.y, ActualWidth(220), TitleTFViewHeight) title:@"会员编号"];
        }
        VWCardView.delegate = self;
        [self.bigScrollView addSubview:VWCardView];
        
        ICMTitleTextFeildView *cardIdView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(ownerPhoneNumberView.rightEdge+xDistanceA, useKindView.y, ActualWidth(220), TitleTFViewHeight) title:@"实物卡号"];
        if (![ICrEAMTool isVW]) cardIdView.hidden = YES;
        [self.bigScrollView addSubview:cardIdView];
        
        self.useKindView = useKindView;
        self.traceTagView = traceTagView;
        self.VWCardView = VWCardView;
        self.cardIdView = cardIdView;
        
        //第五行
        
        ICMTitleTextFeildView *lastDateView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(vinNumberView.x, useKindView.bottomEdge+yDistance, ActualWidth(220), TitleTFViewHeight) title:@"上次进厂日期"];
        [self.bigScrollView addSubview:lastDateView];
        if ([ICrEAMTool isVW]){
        self.FBUView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(currentMileageView.x, lastDateView.y, ActualWidth(220), TitleTFViewHeight) title:@"进口车标识"];
        }else{
         self.FBUView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(customerNameView.rightEdge+xDistanceA, lastDateView.y, ActualWidth(220), TitleTFViewHeight) title:@"进口车标识"];
        }
        [self.bigScrollView addSubview:self.FBUView];

        ICMTitleTextFeildView *memberView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(currentMileageView.rightEdge+xDistanceB, lastDateView.y, ActualWidth(220), TitleTFViewHeight) title:@"经销商会员"];
        if (![ICrEAMTool isVW]) memberView.hidden = YES;
        [self.bigScrollView addSubview:memberView];
        
        ICMTitleTextFeildView *totalPointView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(ownerPhoneNumberView.rightEdge+xDistanceA, lastDateView.y, ActualWidth(220), TitleTFViewHeight) title:@"会员积分"];
        if (![ICrEAMTool isVW]) totalPointView.hidden = YES;
        [self.bigScrollView addSubview:totalPointView];
        self.totalPointView = totalPointView;
        
        self.lastDateView = lastDateView;
       
        self.memberView = memberView;
    if ([ICrEAMTool isVW]){
        self.stepInTimeView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(vinNumberView.x, lastDateView.bottomEdge+yDistance, ActualWidth(220), TitleTFViewHeight) title:@"下次保养日期" addTarget:self selector:@selector(clickCalenderBtn)];
    self.nextMaintainMileage= [ICMTitleTextFeildView createEditableICMTitleTextFeildViewWithFrame:CGRectMake(invoiceDateView.rightEdge+xDistanceA,lastDateView.bottomEdge+yDistance, ActualWidth(220), TitleTFViewHeight) title:@"下次保养里程(KM)"];
    }else{
        self.stepInTimeView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(invoiceDateView.rightEdge+xDistanceA, lastDateView.topEdge, ActualWidth(220), TitleTFViewHeight) title:@"下次保养日期" addTarget:self selector:@selector(clickCalenderBtn)];
        self.nextMaintainMileage= [ICMTitleTextFeildView createEditableICMTitleTextFeildViewWithFrame:CGRectMake(ownerPhoneNumberView.x,lastDateView.topEdge, ActualWidth(220), TitleTFViewHeight) title:@"下次保养里程(KM)"];
        self.nextMaintainMileage.contentTextFeild.keyboardType=UIKeyboardTypeNumberPad;
    }
        
    [self.bigScrollView addSubview: self.stepInTimeView];
       
     [self.bigScrollView addSubview: self.nextMaintainMileage];
        
        
        UIView *firstSeparator = [[UIView alloc] initWithFrame:CGRectMake(self.centerX, 0, 1, self.bigScrollView.height)];
        firstSeparator.backgroundColor = SepColor_gray;
        [self.bigScrollView addSubview:firstSeparator];
        
        UIView *secondSeparetor = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
        secondSeparetor.backgroundColor = SepColor_gray;
        [self.bigScrollView addSubview:secondSeparetor];
        
        UIView *thirdSeparator = [[UIView alloc] initWithFrame:CGRectMake(self.centerX, customerNameView.bottomEdge+(yDistance/2)-7, ScreenWidth/2, 1)];
        thirdSeparator.backgroundColor = SepColor_gray;
        [self.bigScrollView addSubview:thirdSeparator];
        
        UIView *fourthSeparator = [[UIView alloc] initWithFrame:CGRectMake(self.centerX, VWCardView.bottomEdge+(yDistance/2)-7, ScreenWidth/2, 1)];
        fourthSeparator.backgroundColor = SepColor_gray;
        [self.bigScrollView addSubview:fourthSeparator];
        
        
        
        UIButton *carInfoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        carInfoBtn.frame = CGRectMake(ActualWidth(390), ActualHeight(22), ActualWidth(23), ActualWidth(23));
        [carInfoBtn setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:[UIImage imageNamed:Appointment_CarInfo] selectedBackImage:nil actionSEL:@selector(clickCarInfoBtn) target:self];
        [self.bigScrollView addSubview:carInfoBtn];
        [ICrEAMTool createAndAddBiggerButtonForButton:carInfoBtn andSEL:@selector(clickCarInfoBtn) target:self addTop:3 bottom:3 left:3 right:3 inView:self.bigScrollView];
        
        UIButton *monitorBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        monitorBtn.frame = CGRectMake(ActualWidth(460), ActualHeight(22), ActualWidth(23), ActualWidth(23));
        [monitorBtn setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:[UIImage imageNamed:Appointment_CarMonitor] selectedBackImage:nil actionSEL:@selector(clickMonitorBtn) target:self];
        [self.bigScrollView addSubview:monitorBtn];
        [ICrEAMTool createAndAddBiggerButtonForButton:monitorBtn andSEL:@selector(clickMonitorBtn) target:self addTop:3 bottom:3 left:3 right:3 inView:self.bigScrollView];
        
        UIButton *carOwnerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        carOwnerBtn.frame = CGRectMake(ActualWidth(965), ActualHeight(22), ActualWidth(23), ActualWidth(23));
        [carOwnerBtn setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:[UIImage imageNamed:Appointment_CarOwner] selectedBackImage:nil actionSEL:@selector(clickCarOwnerBtn) target:self];
        [self.bigScrollView addSubview:carOwnerBtn];
        [ICrEAMTool createAndAddBiggerButtonForButton:carOwnerBtn andSEL:@selector(clickCarOwnerBtn) target:self addTop:3 bottom:3 left:3 right:3 inView:self.bigScrollView];
        
        UIButton *memberBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        memberBtn.frame = CGRectMake(VWCardView.x+ActualWidth(190), VWCardView.y, ActualWidth(23), ActualWidth(23));
        [memberBtn setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:[UIImage imageNamed:GetCar_Synchronize] selectedBackImage:nil actionSEL:@selector(clickMemberBtn) target:self];
        [self.bigScrollView addSubview:memberBtn];
        [ICrEAMTool createAndAddBiggerButtonForButton:memberBtn andSEL:@selector(clickMemberBtn) target:self addTop:3 bottom:3 left:3 right:3 inView:self.bigScrollView];
        
        self.bigScrollView.contentSize = CGSizeMake(ScreenWidth, totalPointView.bottomEdge);
        [self addSubview:self.bigScrollView];
    }
    return self;
}
#pragma mark    点击日历
- (void)clickCalenderBtn{
    //    [NSDate dateWithTimeIntervalSinceNow:60*24*60*60]
    PACACalendarView *calendarView = [PACACalendarView createCalendarViewWithDidSelectedDate:self.selectedDate minDate:[NSDate date] maxDate:nil andFrame:CGRectMake(ScreenWidth/2-150, ScreenHeight/2-150, ActualWidth(300), ActualWidth(380))];
    calendarView.delegate = self;
    [Window addSubview:calendarView];
    
}
#pragma mark    日历事件
- (void)calendarView:(PACACalendarView *)calendarView didSelectedDate:(NSDate *)selectedDate{
    self.selectedDate = selectedDate;
    NSString *dateString = [selectedDate separatorStyleDateTimeString];
    self.stepInTimeView.contentTextFeild.text = dateString;
    //获得可预约时间列表
}

#pragma mark    清空时间
- (void)delTime:(PACACalendarView *)calendarView{
    self.selectedDate = nil;
    self.stepInTimeView.contentTextFeild.text = @"";
}
+ (instancetype)createICMProxyBasicInfoViewWithFrame:(CGRect)frame{
    return [[ICMProxyBasicInfoView alloc] initWithFrame:frame];
}

#pragma mark 根据model设置页面的值
- (void)setUpWithModel:(ICMProxyBasicInfoModel *)model{
    self.basicInfomodel = model;
    self.vinNumberView.contentText = model.VIN;
    self.plateNumberView.contentText = model.LICENSE;
    self.ownerNameView.contentText = model.OWNER_NAME;
    self.invoiceDateView.contentText = [ICrEAMTool getDateWithTimeInterval:model.SALES_DATE];
    self.currentMileageView.contentText = model.IN_MILEAGE;
    self.ownerPhoneNumberView.contentText = model.MOBILE;
    self.stepInTimeView.contentText=model.NEXT_MAINTAIN_DATE;
    self.nextMaintainMileage.contentText=model.NEXT_MAINTAIN_MILEAGE;
    self.ownerTypeView.contentText = [ICrEAMTool getCodeModelForCode:model.OWNER_PROPERTY].desc;
    self.carTypeView.contentText = [NSString stringWithFormat:@"%@|%@",model.MODEL_CODE,model.MODEL_NAME];
    self.customerNameView.contentText = model.DELIVERER;
    self.customerPhoneNumberView.contentText= model.DELIVERER_MOBILE;
    self.useKindView.contentText = [ICrEAMTool getCodeModelForCode:model.USE_KIND].desc;
    self.traceTagView.contentText = (!model.TRACE_TAG || [model.TRACE_TAG isEqualToString:@""])?@"":[ICrEAMTool getCodeModelForCode:model.TRACE_TAG].desc;
    self.VWCardView.contentText = [ICrEAMTool isVW]?model.HQ_VW_CARD_ID:model.DX_MEMBER_NO;
    self.cardIdView.contentText = model.BAR_CODE;
    self.lastDateView.contentText = (!model.LAST_MAINTAIN_DATE || [model.LAST_MAINTAIN_DATE isEqualToString:@""])?@"":[ICrEAMTool getDateWithTimeInterval:model.LAST_MAINTAIN_DATE];
    self.FBUView.contentText = (!model.IS_FBU || [model.IS_FBU isEqualToString:@""])?@"":[ICrEAMTool getCodeModelForCode:model.IS_FBU].desc;
    self.memberView.contentText = model.MEMBER_NO;
    self.totalPointView.contentText = model.TTL_CONSUME_CREDIT;
    
}

#pragma mark 点击车辆信息按钮
- (void)clickCarInfoBtn{
    if ([self.delegate respondsToSelector:@selector(clickCarInfoBtn)]) {
        [self.delegate clickCarInfoBtn];
    }
    
}

#pragma mark 点击监控信息按钮
- (void)clickMonitorBtn{
    if ([self.delegate respondsToSelector:@selector(clickMonitorBtn)]) {
        [self.delegate clickMonitorBtn];
    }
}

#pragma mark 点击车主信息按钮
- (void)clickCarOwnerBtn{
    if ([self.delegate respondsToSelector:@selector(clickCarOwnerBtn)]) {
        [self.delegate clickCarOwnerBtn];
    }
}

#pragma mark 点击会员信息按钮
- (void)clickMemberBtn{
    if ([self.delegate respondsToSelector:@selector(clickMemberBtn)]) {
        [self.delegate clickMemberBtn];
    }
}

#pragma 页面值变化
//值变化调用此方法
- (void)iCMTitleTextFeildViewValueChanged
{
    self.hasViewValueChanged = YES;
}

- (void)setHasViewValueChanged:(BOOL)hasViewValueChanged
{
    _hasViewValueChanged = hasViewValueChanged;
    if(_hasViewValueChanged)
    {
        //发送变化通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ViewValueChanged" object:self];
    }
}

@end
